package com.antis.olsl.magic.report.annual;

import android.text.TextUtils;

import com.antis.olsl.base.net.BaseSubscriber;
import com.antis.olsl.base.net.ExceptionHandle;
import com.antis.olsl.base.utils.RxUtil;
import com.antis.olsl.library.MyApplication;
import com.antis.olsl.library.utils.RSAEncryptUtil;
import com.antis.olsl.magic.net.ApiServer;
import com.antis.olsl.magic.net.RetrofitManager;
import com.antis.olsl.magic.response.GetSalesSummaryResp;

import java.util.Map;

import timber.log.Timber;

public class AnnualPresenter implements AnnualContract.Presenter {
    private AnnualContract.View mView;
    private ApiServer mApiServer;

    AnnualPresenter() {
        mApiServer = RetrofitManager.getInstance().getServerApi();
    }

    @Override
    public void takeView(AnnualContract.View view) {
        mView = view;
    }

    @Override
    public void getYearSalesSummary(Map<String, Object> map) {
        String encryptString = RSAEncryptUtil.generateEncryptStr(map);
        Timber.tag("hhh").e("getYearSalesSummary map= " + map);
        String token = null != MyApplication.getUserInfo() && !TextUtils.isEmpty(MyApplication.getUserInfo().getToken()) ? MyApplication.getUserInfo().getToken() : "123456";

        mApiServer.getYearSalesSummary(encryptString, token, 2).compose(RxUtil.<GetSalesSummaryResp>applySchedulers(RxUtil.IO_ON_UI_TRANSFORMER_BACK_PRESSURE))
                .compose(mView.<GetSalesSummaryResp>bindToLife())
                .subscribe(new BaseSubscriber<GetSalesSummaryResp>() {
                    @Override
                    public void onNext(GetSalesSummaryResp resp) {
                        if (resp.isOk()) {
                            if (null != resp.content && resp.content.size() > 0) {
                                mView.getYearSalesSummarySuccess(resp.content.get(0));
                            } else {
                                mView.getYearSalesSummaryEmpty();
                            }
                        } else if (resp.isTimeOut()) {
                            mView.getYearSalesSummaryFailure("请求超时");
                        } else if (resp.isSlowNetwork()) {
                            mView.getYearSalesSummaryFailure("网络缓慢，稍后重试");
                        } else if (resp.isUnauthorized()) {
                            mView.getYearSalesSummaryFailure("接口未授权");
                        } else {
                            mView.getYearSalesSummaryFailure("请求失败");
                        }
                    }

                    @Override
                    protected void hideDialog() {

                    }

                    @Override
                    protected void showDialog() {

                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseThrowable e) {
                        if (null != e) {
                            if (!TextUtils.isEmpty(e.message)) {
                                mView.getYearSalesSummaryFailure(e.message);
                            } else {
                                mView.getYearSalesSummaryFailure("请求失败");
                            }
                        } else {
                            mView.getYearSalesSummaryFailure("请求失败");
                        }
                    }
                });
    }

    @Override
    public void subscribe() {

    }

    @Override
    public void unSubscribe() {
        Timber.tag("AnnualPresenter").e("unSubscribe");
        mView = null;
    }
}
